<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Maven2 Java Formatter Plugin - Examples</title>
    <style type="text/css" media="all">
      @import url("./css/maven-base.css");
      @import url("./css/maven-theme.css");
      @import url("./css/site.css");
    </style>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
          <meta name="author" content="Matt Blanchette" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                    <span id="bannerLeft">
    
            Maven2 Java Formatter Plugin
    
            </span>
                    <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
    
            <div class="xleft">
        Last Published: 2011-03-04
                      </div>
            <div class="xright">      
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
    
                   <h5>Overview</h5>
            <ul>
              
    <li class="none">
                    <a href="http://code.google.com/p/maven-java-formatter-plugin/" class="externalLink">Project Home</a>
          </li>
              
    <li class="none">
                    <a href="index.html">Introduction</a>
          </li>
              
    <li class="none">
                    <a href="plugin-info.html">Goals</a>
          </li>
              
    <li class="none">
                    <a href="usage.html">Usage</a>
          </li>
          </ul>
              <h5>Examples</h5>
            <ul>
              
    <li class="none">
                    <a href="examples.html#Setting_Source_Files">Source Files</a>
          </li>
              
    <li class="none">
                    <a href="examples.html#Setting_Compiler_Version">Compiler Version</a>
          </li>
              
    <li class="none">
                    <a href="examples.html#System_Independent_Line_Endings">Line Endings</a>
          </li>
              
    <li class="none">
                    <a href="examples.html#Source File Encoding">File Encoding</a>
          </li>
              
    <li class="none">
                    <a href="examples.html#Custom_Configuration_File">Custom Formatting</a>
          </li>
              
    <li class="none">
                    <a href="examples.html#Multimodule_Configuration">Multimodule</a>
          </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
              
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
              
      
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
                                           <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img alt="Built by Maven" src="./images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
    
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2>Examples</h2>
<p>Here are more examples of using the java formatter plugin.</p>
<p>For more details on these parameters, see the <a href="format-mojo.html">format goal</a></p>
<div class="section"><h3><a name="Setting_Source_Files">Setting Source Files</a></h3>
<p>To specify the locations of java source files to format, use the <tt>includes</tt> and <tt>excludes</tt> parameters.<br />
These parameters are fileset patterns relative to the source and test source directories, usually <tt>src/main/java</tt> and <tt>src/test/java</tt>.<br />
By default, the plugin formats all java source files in the source and test source directories.<br />
This example only formats files in the <tt>include</tt> package, except for test classes and those in the <tt>exclude</tt> &quot;special&quot; subpackage:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;includes&gt;
      &lt;include&gt;com/relativitas/maven/plugins/formatter/&lt;/include&gt;
    &lt;/includes&gt;
    &lt;excludes&gt;
      &lt;exclude&gt;com/relativitas/maven/plugins/formatter/special/&lt;/exclude&gt;
      &lt;exclude&gt;**/*Test.java&lt;/exclude&gt;
    &lt;/excludes&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
</div>
<div class="section"><h3><a name="Setting_Compiler_Version">Setting Compiler Version</a></h3>
<p>To specify the compiler version to use in formatting, use the <tt>compilerSource</tt>, <tt>compilerCompliance</tt>, and <tt>compilerTargetPlatform</tt> parameters.<br />
By default, the plugin uses a version of 1.5 for all of these parameters.<br />
This example uses 1.6 as the compiler version during formatting:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;compilerSource&gt;1.6&lt;/compilerSource&gt;
    &lt;compilerCompliance&gt;1.6&lt;/compilerCompliance&gt;
    &lt;compilerTargetPlatform&gt;1.6&lt;/compilerTargetPlatform&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
</div>
<div class="section"><h3><a name="System_Independent_Line_Endings">System Independent Line Endings</a></h3>
<p>To specify the line ending to use in formatting, use the <tt>lineEnding</tt> parameter.<br />
By default, the plugin uses the line ending of the current system.<br />
This parameter allows using the existing line ending of files or a consistent line ending for all files, useful for large projects with developers using different operating systems.<br />
This example specifies using the carriage-return and line-feed of DOS/Windows for the line ending during formatting:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;lineEnding&gt;CRLF&lt;/lineEnding&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
<p>For all possible values, see the <tt>lineEnding</tt> description on the <a href="format-mojo.html">format goal</a></p>
</div>
<div class="section"><h3><a name="Source_File_Encoding">Source File Encoding</a></h3>
<p>To specify the encoding to use during formatting, use the <tt>encoding</tt> parameter.<br />
By default, the plugin uses the <tt>project.build.sourceEncoding</tt> property if specified in the pom.<br />
Otherwise, the file encoding of the current system will be used.<br />
This example specifies using the UTF-8 encoding during formatting using the parameter:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;encoding&gt;UTF-8&lt;/encoding&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
<p>This example specifies also using the UTF-8 encoding during formatting but using the property:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
&lt;/plugin&gt;
...
&lt;properties&gt;
  &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;/properties&gt;</pre>
</div>
</div>
<div class="section"><h3><a name="Custom_Configuration_File">Custom Configuration File</a></h3>
<p>By default, the plugin uses the Eclipse formatter settings and provides no additional preferences other than the compiler version.<br />
To specify formatting preferences for the Eclipse code formatter, use the <tt>configFile</tt> parameter.<br />
This parameter points to a file or classpath resource location for an Eclipse code formatter xml file.</p>
<p>A <tt>configFile</tt> can be created and exported in Eclipse as follows:</p>
<ul><li>In Eclipse, go to <tt>Window</tt> &gt; <tt>Preferences</tt> &gt; <tt>Java</tt> &gt; <tt>Code Style</tt> &gt; <tt>Formatter</tt></li>
<li>If you have a custom profile, select the profile and click on the <tt>Edit</tt> button<ul><li>To create a custom profile, click the <tt>New</tt> button</li>
<li>Enter a name and select an existing profile to inherit settings from</li>
<li>Click <tt>Ok</tt> and set the profile settings</li>
</ul>
</li>
<li>Click the <tt>Export</tt> button and save the file</li>
</ul>
<p>This file should contain a profile of kind <tt>CodeFormatterProfile</tt> with <tt>org.eclipse.jdt.core.formatter</tt> settings:</p>
<div class="source"><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;profiles version=&quot;11&quot;&gt;
  &lt;profile kind=&quot;CodeFormatterProfile&quot; name=&quot;Custom Project Formatter&quot; version=&quot;11&quot;&gt;
    &lt;setting id=&quot;org.eclipse.jdt.core.formatter.blank_lines_after_package&quot; value=&quot;1&quot;/&gt;
    &lt;setting id=&quot;org.eclipse.jdt.core.compiler.source&quot; value=&quot;1.6&quot;/&gt;
    ...
  &lt;/profile&gt;
&lt;/profiles&gt;</pre>
</div>
<p>This example uses a configuration file named <i>eclipse-formatter-config.xml</i> for formatting:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;configFile&gt;${project.basedir}/eclipse-formatter-config.xml&lt;/configFile&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
<p>By default, the plugin uses the compiler versions in the configFile instead of the compiler parameters.<br />
To specify using the compiler parameters instead of those in the configFile, set the <tt>overrideConfigCompilerVersion</tt> parameter to <tt>true</tt>.</p>
<p>This parameter is useful to avoid updating the configFile to keep the compiler version in sync with the pom.<br />
This example uses all settings except the compiler version defined in the configuration file named <i>eclipse-formatter-config.xml</i> and instead uses the compiler version specified in the parameters for formatting:</p>
<div class="source"><pre>&lt;plugin&gt;
  &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
  &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
  &lt;version&gt;0.3.1&lt;/version&gt;
  &lt;configuration&gt;
    &lt;configFile&gt;${project.basedir}/eclipse-formatter-config.xml&lt;/configFile&gt;
    &lt;overrideConfigCompilerVersion&gt;true&lt;/overrideConfigCompilerVersion&gt;
    &lt;compilerSource&gt;1.6&lt;/compilerSource&gt;
    &lt;compilerCompliance&gt;1.6&lt;/compilerCompliance&gt;
    &lt;compilerTargetPlatform&gt;1.6&lt;/compilerTargetPlatform&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
</div>
</div>
<div class="section"><h3><a name="Multimodule_Configuration">Multimodule Configuration</a></h3>
<p>This section describes configuring the plugin for use within multimodule projects using a module containing the configuration file resource.</p>
<p>This example uses a project called <i>multiproject</i> with the following structure:</p>
<div class="source"><pre>multiproject
|-- pom.xml
|-- moduleA
|   `-- pom.xml
|-- moduleB
|   `-- pom.xml</pre>
</div>
<p><b>Create new project for the shared resource</b></p>
<p>Add another project for the common configuration resources, called <i>build-tools</i> in this example.</p>
<div class="source"><pre>multiproject
|-- pom.xml
|-- build-tools
|   |-- src
|   |   `-- main
|   |       `-- resources
|   |           `-- eclipse
|   |               `-- formatter.xml
|   `-- pom.xml
|-- moduleA
|-- moduleB</pre>
</div>
<p><b>Configure the other projects to use it</b></p>
<p>Include the java formatter plugin in the top level <tt>pom.xml</tt>.</p>
<p>Specify a plugin dependency on <tt>build-tools</tt> in the <tt>&lt;build&gt;</tt> element of your <tt>pom.xml</tt>.</p>
<div class="source"><pre>&lt;project&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.googlecode.example.multiproject&lt;/groupId&gt;
  &lt;artifactId&gt;multiproject-parent&lt;/artifactId&gt;
  &lt;version&gt;1.0&lt;/version&gt;
  &lt;packaging&gt;pom&lt;/packaging&gt;
  &lt;name&gt;Multiproject Parent&lt;/name&gt;
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;com.googlecode.maven-java-formatter-plugin&lt;/groupId&gt;
        &lt;artifactId&gt;maven-java-formatter-plugin&lt;/artifactId&gt;
        &lt;version&gt;0.3.1&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;format&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;configFile&gt;eclipse/formatter.xml&lt;/configFile&gt;
        &lt;/configuration&gt;
        &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;com.googlecode.example.multiproject&lt;/groupId&gt;
            &lt;artifactId&gt;build-tools&lt;/artifactId&gt;
            &lt;version&gt;1.0&lt;/version&gt;
          &lt;/dependency&gt;
        &lt;/dependencies&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;modules&gt;
    &lt;module&gt;build-tools&lt;/module&gt;
    &lt;module&gt;moduleA&lt;/module&gt;
    &lt;module&gt;moduleB&lt;/module&gt;
  &lt;/modules&gt;
&lt;/project&gt;</pre>
</div>
<p>Based on this configuration, the value of <tt>configFile</tt> will be resolved from the classpath, specifically from the <i>build-tools</i> JAR. Adding <i>build-tools</i> as a dependency on the plugin included it in the classpath.</p>
</div>
</div>

      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2011
    
          
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
